<?php
/**
 *  extention.func.php 用户自定义函数库
 *
 * @copyright			(C) 2005-2010 PHPCMS
 * @license				http://www.phpcms.cn/license/
 * @lastmodify			2010-10-27
 */

/**
 * 获取频道分类more连接
 * @param $catid   频道分类id
 * @param $siteid  站点id
 */
function more_url($catid = NULL, $siteid = '') {
	if (empty($siteid)) $siteid = get_siteid();
		$category = getcache('category_content_'.$siteid,'commons');
		foreach($category as $k=>$v) {
			if($v['siteid'] == $siteid && ($catid === NULL || $v['catid'] == $catid)) $url=$v['url']; 
		 }
	return $url;
}

/**
 * 获取父级频道信息
 * @param $catid   频道分类id
 * @param $siteid  站点id
 */
function get_parentcat($catid = NULL, $siteid = '') {
	if (empty($siteid)) $siteid = get_siteid();
	$category = getcache('category_content_'.$siteid,'commons');
	$this_cat = $category[$catid];
	$preant_cat = $category[$this_cat['parentid']];
	return $preant_cat;
}

/**
 * 获取频道名
 * @param $catid   频道分类id
 * @param $siteid  站点id
 */
function get_catname($catid = NULL, $siteid = ''){
	if (empty($siteid)) $siteid = get_siteid();
	$category = getcache('category_content_'.$siteid,'commons');
	foreach($category as $k=>$v) {
		if($v['siteid'] == $siteid && ($catid === NULL || $v['catid'] == $catid)) $catname=$v['catname'];
	}
	return $catname;
}

/**
 * 获取频道所有分类的文章数
 * @param $catid   频道id
 * @param $siteid  站点id
 */
function get_items($catid = NULL,$siteid = ''){
	if(empty($siteid)) $siteid = get_siteid();
	$category = getcache('category_content_'.$siteid,'commons');
	foreach($category as $k=>$v){
		if($v['siteid'] == $siteid && ($catid === NULL || $v['catid'] == $catid)) $arrchildid = $v['arrchildid'];
	}
	$arrchildid = explode(',',$arrchildid);
	$count_items = 0;
	foreach($arrchildid as $key=>$value){
		$count_items += $category[$value]['items'];
	}

	return $count_items;
}

/**
 * 简写频道名，去掉手机型号
 * @param $catname   频道名
 *
 */
function short_catname($catname = '', $parentcatname = ''){
	if(!empty($catname) && !empty($parentcatname)){
		$catname = explode($parentcatname,$catname);
	}

	return $catname['1'];
}

/**
 * 拼装SEO优化频道名，用于频道链接
 * @param $catename 频道名
 *
 */
function seotitle_catname($catname = ''){
	if(!empty($catname)){
		$return_catname = explode('/', $catname);
		if($return_catname[0]){
			if($return_catname[1]){
				//判断是否为中文来确定是否在"ROM"前加空格，用于美化
				if (preg_match("/^[\x7f-\xff]+$/", $return_catname[1])) {
					$return_catname[1] = $return_catname[1].'ROM';
				}else{
					$return_catname[1] = $return_catname[1].' ROM';
				}
				return $return_catname[0].' ROM|'.$return_catname[1];
			}else{
				return $return_catname[0].' ROM';
			}
		}else{
			return $catname;
		}
	}
}

/**
 * 获取频道信息
 * @param $catid 频道id
 *
 */
 function get_catinfo($catid, $siteid=''){
 	if(empty($siteid)) $siteid = get_siteid();
	$category = getcache('category_content_'.$siteid,'commons');
	$catinfo = $category[$catid];
	return $catinfo;
 }
 
/**
 * 获取最新ROM频道信息
 * @param $catid 频道id
 * @param $num 个数
 * @param $siteid 站点ID
 *
 */
function get_newrom($catid, $num = 1, $siteid=''){
	if(empty($siteid)) $siteid = get_siteid();
	$category = getcache('category_content_'.$siteid,'commons');
	$rom_category = $category[$catid];
	$arrchildid = explode(',',$rom_category['arrchildid']);
	$arrchild = $result = array();
	foreach($arrchildid as $key => $value){
		if($category[$value]['child'] != 0 && $category[$value]['image'] != '' && $category[$value]['catid'] != $catid){
			$arrchild[$value] = $category[$value];
		}
	}
	krsort($arrchild);
	$result = array_slice($arrchild, 0, $num);
	
	return $result;
}

/**
 * 统计频道所有文章的点击数
 * @param $catid 频道id
 *
 */
function get_cathits($catid, $siteid = ''){
	if(empty($siteid)) $siteid = get_siteid();
	$category = getcache('category_content_'.$siteid,'commons');
	$rom_category = $category[$catid];
	$db = pc_base::load_model('hits_model');
	$data = "select sum(`views`) as `sum_views`";
	$where = "`catid` in (".$rom_category['arrchildid'].")";
	$r = $db->get_one($where, $data);
	$result = $r['sum_views'];
	return $result;
}

/**
 * 修正点击统计的频道
 * @param $catid 频道id
 *
 */
function update_hits_cat($catid = '', $siteid = ''){
    $hits_db = pc_base::load_model('hits_model');
    $content_db = pc_base::load_model('content_model');
    if(empty($siteid)) $siteid = get_siteid();
    $categorys = getcache('category_content_'.$siteid,'commons');
    if($catid){
	$where = "`catid` = $catid";
    }
    $r = $hits_db->select($where);
    
    foreach($r as $key => $value){
	$hitsid_arr = explode('-', $value['hitsid']);
	$modelid = $hitsid_arr[1];
	$art_id = $hitsid_arr[2];
	//设置模型数据表名
	$content_db->set_model($modelid);
	$table_name = $content_db->table_name;
	$where = "`id` = '".$art_id."'";
	$rs = $content_db->get_one($where);
	if($rs['catid'] != $value['catid']){
	    $hits_db->update(array('catid'=>$rs['catid']),array('hitsid'=>$value['hitsid']));
	}
    }
    
}
?>
